﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Data;
using GPSTracker.Views.DAL;
using DevExpress.Web.ASPxEditors;
using System.Data.SqlClient;
using GPSTracker.Helper;


namespace GPSTracker.Views.Report
{
    public partial class TempoTracking : System.Web.UI.Page
    {
        private TempoTrackingDAL objTempTracDAL = new TempoTrackingDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    BindTempo();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void BindTempo()
        {
            try
            {
                var dt = new DataTable();
                dt = objTempTracDAL.FillTempo();

                if (dt.Rows.Count > 0)
                {
                    cmbTempo.DataSource = dt;
                    cmbTempo.TextField = "TempoNo";
                    cmbTempo.ValueField = "TempoID";
                    cmbTempo.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void cmb_DataBound(object sender, EventArgs e)
        {
            var list = sender as ASPxComboBox;
            var lEi = new ListEditItem();
            lEi.Text = "--Select--";

            if (list != null)
            {
                list.Items.Insert(0, lEi);
            }

            list.SelectedIndex = 0;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                var strTempoID = string.Empty;
                var dtFromDate = System.DateTime.Now;
                var dtToDate = System.DateTime.Now;

                if (cmbTempo.Text != "--Select--")
                {
                    strTempoID = cmbTempo.Value.ToString();
                }
                else
                {
                    strTempoID = null;
                }

                if (dtpFromDate.Text != string.Empty)
                {
                    dtFromDate = Convert.ToDateTime(dtpFromDate.Value);
                }

                if (dtpToDate.Text != string.Empty)
                {
                    dtToDate = Convert.ToDateTime(dtpToDate.Value);
                }

                var dt = new DataTable();
                var cmd = new SqlCommand("SP_GetGPSData", CommonFx.GetSqlCon());
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@pTempoID", strTempoID);

                cmd.Parameters.AddWithValue("@pFromDttm", dtFromDate);
                cmd.Parameters.AddWithValue("@pToDttm", dtToDate);

                var da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", this.PlotGPSPoints(dt), false);
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "TempoTracking", "btnSearch_Click");
            }
        }

        public String PlotGPSPoints(DataTable tblPoints)
        {
            try
            {
                var Locations = string.Empty;
                var sJScript = string.Empty;
                var i = 0;

                foreach (DataRow r in tblPoints.Rows)
                {
                    if (r["latitude"].ToString().Trim().Length == 0)
                    {
                        continue;
                    }

                    var Latitude = r["latitude"].ToString();
                    var Longitude = r["longitude"].ToString();

                    Locations += Environment.NewLine + @"
                path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));

                var marker" + i.ToString() + @" = new google.maps.Marker({
                    position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),
                    title: '#' + path.getLength(),
                    map: map
                });";
                    i++;
                }

                sJScript = @"<script type='text/javascript'>

                var poly;
                var map;

                var cmloc = new google.maps.LatLng(" + tblPoints.Rows[0]["Latitude"].ToString() + ", " + tblPoints.Rows[0]["Longitude"].ToString() + @") 
                
                var myOptions = {
                    zoom: " + CommonDAL.AttributeDAL.GetAttributeByType("GoogleMap", "ZoomLevel").Rows[0]["Value"].ToString() + @",
                    center: cmloc,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

                var polyOptions = {
                    strokeColor: 'blue',
                    strokeOpacity: 0.5,
                    strokeWeight: 3
                }
                poly = new google.maps.Polyline(polyOptions);
                poly.setMap(map);

                var path = poly.getPath();

               " + Locations + @"

                </script>";

                return sJScript;
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "TempoTracking", "PlotGPSPoints");
                return null;
            }
        }
    }
}
